//
//  LGFollowOmitReasonView.m
//  HeartDisease_YHJK
//
//  Created by 1234 on 2025/9/12.
//  随访计划 -- 忽略理由 -- 弹框

#import "LGFollowOmitReasonView.h"

@interface LGFollowOmitReasonView()<UITextViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UITextView *textView;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;

@end

@implementation LGFollowOmitReasonView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboardAction)]];
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        [self createSubView];
        [UIView animateWithDuration:0.4 animations:^{
            self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.8];
            self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 1000, 1000);
        }];
        [self.textView becomeFirstResponder];
    }
    return self;
}

-(void)sureButtonAction{
    if ([self.textView.text isEmptyString]) {
        [TooltipView showMessage:@"请输入忽略理由" offset:0];
        return;
    }
    if (self.omitReasonAction) {
        self.omitReasonAction(self.textView.text);
    }
    [self closeViewAnimation];
}

-(void)closeViewAnimation{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 0.001, 0.001);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)closeKeyboardAction{
    [self.textView resignFirstResponder];
}

- (void)textViewDidChange:(UITextView *)textView{
    NSString *lang = self.textInputMode.primaryLanguage; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [self.textView markedTextRange];
        if (!selectedRange) { // 没有高亮
            NSString *content = (textView.text.length>20)?[textView.text substringToIndex:20]:textView.text;
            self.textView.text = content;
            self.numberLabel.text = [NSString stringWithFormat:@"%ld/20",content.length];
        }
    }else{
        NSString *content = (textView.text.length>20)?[textView.text substringToIndex:20]:textView.text;
        self.textView.text = content;
        self.numberLabel.text = [NSString stringWithFormat:@"%ld/20",content.length];
    }
    
    
}

-(void)createSubView{
    [self addSubview:self.baseView];
    UILabel *titleLabel = [UILabel lableWithText:@"请填写忽略理由：" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    titleLabel.frame = CGRectMake(viewPix(24), viewPix(24), viewPix(150), viewPix(25));
    [self.baseView addSubview:titleLabel];
    [self.baseView addSubview:self.textView];
    [self.baseView addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.textView).offset(-viewPix(10));
    }];
    UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(0, viewPix(185), viewPix(320), 0.5)];
    lineView1.backgroundColor = [UIColor colorWithString:@"#E5E5E5"];
    [self.baseView addSubview:lineView1];
    
    UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(viewPix(160), viewPix(185), 1, viewPix(55))];
    lineView2.backgroundColor = [UIColor colorWithString:@"#E5E5E5"];
    [self.baseView addSubview:lineView2];
    
    UIButton *cancelBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFont(17) textColor:@"#282828" frame:CGRectMake(0, viewPix(185), viewPix(160), viewPix(55))];
    [cancelBtn addTarget:self action:@selector(closeViewAnimation) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:cancelBtn];
    
    UIButton *sureBtn = [UIButton buttonWithTitle:@"确定" titleFont:LGFontWeight(17, UIFontWeightMedium) textColor:@"#5E8EF5" frame:CGRectMake(viewPix(160), viewPix(185), viewPix(160), viewPix(55))];
    [sureBtn addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:sureBtn];
    
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake((Screen_W-viewPix(320))/2.0, (Screen_H-viewPix(240))/2.0-viewPix(20), viewPix(320), viewPix(240))];
        _baseView.transform = CGAffineTransformScale(_baseView.transform, 0.001, 0.001);
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 8;
    }
    return _baseView;
}

-(UITextView *)textView{
    if(!_textView){
        NSLayoutManager *manager = [[NSLayoutManager alloc]init];
        NSTextContainer *container = [[NSTextContainer alloc]initWithSize:CGSizeMake(viewPix(252), viewPix(70))];
        [container setLayoutManager:manager];

//        _textView = [[UITextView alloc]initWithFrame:CGRectMake(viewPix(24), viewPix(60), viewPix(272), viewPix(90)) textContainer:container];
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(viewPix(24), viewPix(60), viewPix(272), viewPix(90))];
        _textView.tintColor = [UIColor colorWithString:@"#333333"];
        _textView.textColor = [UIColor colorWithString:@"#333333"];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.font = LGFont(15);
        _textView.delegate = self;
        [_textView addBorder:@"#F2F2F2" lineWidth:1 cornerRidus:8];
    }
    return _textView;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"0/20" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _numberLabel;
}





@end
